
package trabalhotec.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.PropertyVetoException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;

public class Clientes extends JInternalFrame {
    JTextField Nome;
    JTextField CPF;
    JTextField Email;
    JRadioButton masculino;
    JRadioButton feminino;
    JPanel pane;
    JPanel buscar;
    JButton Procura;
    JButton limpardados;
    JButton Sair;
    JTable busc;
    trabalhotec.Negocios.Clientes cliente = new trabalhotec.Negocios.Clientes();
    Clientes(){
        this.setTitle("Cadastro de clientes");
        pane = new JPanel();
        addClientes();
        this.setClosable(true);
        this.setMaximizable(false);
        this.setIconifiable(true);
        this.setResizable(false);
        this.getContentPane().add(pane);
        
    }
    public void addClientes(){
        pane.removeAll();
        JLabel nome = new JLabel("Nome:");
        JLabel cpf = new JLabel("CPF:");
        JLabel email = new JLabel("Email:");
        JLabel sexo = new JLabel("Sexo:");
        Nome = new JTextField(36);
        CPF = new JTextField(20);
        Email = new JTextField(20);
        masculino = new JRadioButton("Masculino");
        feminino = new JRadioButton("Feminino");
        JButton Cadastrar = new JButton("Cadastrar");
        JButton LimparDados = new JButton("Limpar Dados");
        Sair = new JButton("Sair");
        pane.setLayout(new GridBagLayout());
        ButtonGroup sexos = new ButtonGroup();
        sexos.add(masculino);
        sexos.add(feminino);
        GridBagConstraints c = new GridBagConstraints();
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        pane.add(nome,c);
        c.gridx=1;
        c.gridwidth = 3;
        pane.add(Nome,c);
        c.gridwidth =1;
        c.gridx=0;
        c.gridy=1;
        pane.add(cpf,c);
        c.gridx=1;
        pane.add(CPF,c);
        c.gridx=2;
        pane.add(sexo,c);
        c.gridx=3;
        pane.add(masculino,c);
        c.gridy=2;
        pane.add(feminino,c);
        c.gridx=0;
        c.gridy=2;
        pane.add(email,c);
        c.gridx=1;
        pane.add(Email,c);
        JPanel botoes = new JPanel();
        botoes.setLayout(new FlowLayout());
        botoes.add(Cadastrar);
        botoes.add(LimparDados);
        botoes.add(Sair);
        CPF.addKeyListener(new KeyListener(){

            public void keyTyped(KeyEvent e) {
                if (CPF.getText().length() >= 11 || !Character.isDigit(e.getKeyChar()) ) {
                    e.consume();
                }
            }

            public void keyPressed(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
            
        });
        Cadastrar.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                char s;
                if(Nome.getText().equals(null)||Nome.getText().equals("")||CPF.getText().equals(null)||CPF.getText().equals("")||Email.getText().equals(null)||Email.getText().equals("")) JOptionPane.showMessageDialog(null, "Algum campo em branco");
                else {
                    if(masculino.isSelected()) s = 'm';
                    else s='f';
                    if(cliente.cadCli(Long.parseLong(CPF.getText()), Nome.getText(), Email.getText() , s)){
                        Object[] options = { "Sim", "Não" };  
                        int i = JOptionPane.showOptionDialog(null,"Cliente cadastrado, Cadastrar outro cliente?", "Saída", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);  
                        if (i == JOptionPane.YES_OPTION);
                        else try {
                            setClosed(true);
                            } catch (PropertyVetoException ex) {
                            Logger.getLogger(Clientes.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }else JOptionPane.showMessageDialog(null, "CPF Ja cadastrado.");
                }
            }
        });
        LimparDados.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                Nome.setText(null);
                CPF.setText(null);
                Email.setText(null);
            }
            
        });
        Sair.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setClosed(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(Voos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        c.gridy=3;
        c.gridx=0;
        c.gridwidth=3;
        pane.add(botoes,c);
        this.pack();
        this.repaint();
        this.revalidate();
    }
    public void resultBusca(String name,Long cod){
        pane.removeAll();
        pane.setLayout(new BorderLayout());
        this.setTitle("Resultado da busca");
        Vector<Vector<String>> valores = cliente.buscaClientes(name, cod);
        System.out.println(cliente.buscaClientes(name, cod));
        Vector<String> titulos = new Vector<String>();
        titulos.add("Nome");
        titulos.add("CPF");
        titulos.add("Email");
        titulos.add("Sexo");
        busc = new JTable(valores,titulos);
        busc.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane paneBusc = new JScrollPane(busc);
        pane.add(paneBusc,BorderLayout.PAGE_START);
        JPanel botoes = new JPanel();
        botoes.setLayout(new FlowLayout());
        JButton Visu = new JButton("Visualizar voos");
        Sair = new JButton("Sair");
        JButton novaconsul = new JButton("Nova consulta");
        botoes.add(Visu);
        botoes.add(novaconsul);
        botoes.add(Sair);
        Visu.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                int selRow = busc.getSelectedRow();
                if(selRow ==-1)JOptionPane.showMessageDialog(null, "Nenhuma cliente selecionado");
                else{
                    Long valor = Long.parseLong((String) busc.getModel().getValueAt(selRow, 1));
                    voosdecliente(valor);
                }
            }
            
        });
        Sair.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setClosed(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(Voos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        novaconsul.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                buscarCliente();
            }
            
        });
        pane.add(botoes,BorderLayout.CENTER);
        
        
        this.validate();
        this.repaint();
        this.pack();
    }
    public void voosdecliente(Long valor){
        pane.removeAll();
        buscar = new JPanel();
        this.setTitle("Voos de cliente");
        Vector<Vector<String>> valores = cliente.buscavoos(valor);
        Vector<String> titulos = new Vector<String>();
        titulos.add("Codigo");
        titulos.add("Origem");
        titulos.add("Destino");
        titulos.add("Data de partida");
        titulos.add("Data de chegada");
        titulos.add("Tempo de voo");
        busc = new JTable(valores,titulos);
        busc.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane paneBusc = new JScrollPane(busc);
        pane.setLayout(new BorderLayout());
        pane.add(paneBusc, BorderLayout.PAGE_START);
        Sair = new JButton("Sair");
        Sair.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setClosed(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(Voos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        pane.add(Sair,BorderLayout.CENTER);
        this.revalidate();
        this.repaint();
        this.pack();
    }
    public void buscarCliente(){
        pane.removeAll();
        buscar = new JPanel();
        this.setTitle("Visualizar voo de cliente");
        JLabel titulo = new JLabel("Digite o nome ou o CPF do cliente");
        JLabel nome = new JLabel("Nome:");
        JLabel cpf = new JLabel("CPF:");
        JLabel ou = new JLabel("ou");
        Nome = new JTextField(32);
        CPF = new JTextField(20);
        pane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth=5;
        c.anchor = GridBagConstraints.FIRST_LINE_START;
        pane.add(titulo,c);
        c.gridwidth=1;
        c.gridy=1;
        pane.add(nome,c);
        c.gridx=1;
        pane.add(Nome,c);
        c.gridx=0;
        c.gridy=2;
        pane.add(ou,c);
        c.gridx=0;
        c.gridy=3;
        pane.add(cpf,c);
        c.gridx=1;
        pane.add(CPF,c);
        c.gridy=4;
        c.gridwidth=3;
        c.gridx=0;
        JPanel botoes = new JPanel();
        Procura = new JButton("Procurar");
        limpardados = new JButton("Limpar dados");
        Sair = new JButton("Sair");
        botoes.setLayout(new FlowLayout());
        botoes.add(Procura);
        botoes.add(limpardados);
        botoes.add(Sair);
        pane.add(botoes,c);
        CPF.addKeyListener(new KeyListener(){

            public void keyTyped(KeyEvent e) {
                if (CPF.getText().length() >= 11 || !Character.isDigit(e.getKeyChar()) ) {
                    e.consume();
                }
            }

            public void keyPressed(KeyEvent e) {
            }

            public void keyReleased(KeyEvent e) {
            }
            
        });
        Procura.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                if((Nome.getText().equals(""))&&CPF.getText().equals("")) JOptionPane.showMessageDialog(null, "Especifique pelo menos um campo para a busca");
                else {
                    String name;
                    Long cod;
                    if(Nome.getText().equals("")) name = null;
                    else name = Nome.getText();
                    if(CPF.getText().equals("")) cod = null;
                    else cod = Long.parseLong(CPF.getText());
                    if(cliente.buscaClientes(name, cod).size() == 0)JOptionPane.showMessageDialog(null, "nenhum cliente encontrado");
                    else resultBusca(name,cod);
                    
                }
            }
            
        });
        limpardados.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e) {
                Nome.setText(null);
                CPF.setText(null);
                Email.setText(null);
            }
            
        });
        Sair.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setClosed(true);
                } catch (PropertyVetoException ex) {
                    Logger.getLogger(Voos.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            
        });
        this.pack();
        this.repaint();
        this.revalidate();
    }

}
